#! C:\Octave\3.2.4_gcc-4.4.0\bin -qf
printf ("%s\n", program_name ());
arg_list = argv ();
nopause = 1;
nographic = 1;
maxIter = 250;
hidden_layer_size = 75;   % 25 hidden units
image_size = 27;

for i = 1:nargin
  if (strcmp(arg_list{i},"no_pause") == 1)
		nopause = 1;
  elseif(strcmp(arg_list{i}, "no_graphic") == 1)
		nographic = 1;
  elseif(strcmp(arg_list{i}, "max_iter") == 1)
		maxIter = str2num(arg_list{i + 1});
		i = i + 1;
  elseif(strcmp(arg_list{i}, "hidden_layer_size") == 1)
		hidden_layer_size = str2num(arg_list{i + 1});
		i = i + 1;
  elseif(strcmp(arg_list{i}, "image_size") == 1)
		image_size = str2num(arg_list{i + 1});
		i = i + 1;
  endif
endfor


%% Initialization
%clear ; close all; clc

%% Setup the parameters you will use for this exercise

input_layer_size  = image_size;  % 20x20 Input Images of Digits
num_labels = 3;          % 2 labels, from 1 to 2   

%% =========== Part 1: Loading and Visualizing Data =============
%  We start the exercise by first loading and visualizing the dataset. 
%  You will be working with a dataset that contains handwritten digits.
%

% Load Training Data
fprintf('Loading and Visualizing Data ...\n')

%load('ex4data1.mat');
data = load('karcinom.m');
X = data(:, (1:input_layer_size)); 

y = data(:, input_layer_size + 1);
% number of training examples
% m = length(y); 
m = size(X, 1);
%for i=1:m,
%	y(i) = y(i) + 1;
%end

% Randomly select 100 data points to display
sel = randperm(size(X, 1));
sel = sel(1:m);

if(nographic == 0)
	displayData(X(sel, :));
endif

if(nopause == 0)
	fprintf('Program paused. Press enter to continue.\n');
	pause;
endif;

fprintf('\nInitializing Neural Network Parameters ...\n')

initial_Theta1 = randInitializeWeights(input_layer_size, hidden_layer_size);
initial_Theta2 = randInitializeWeights(hidden_layer_size, num_labels);

% Unroll parameters
initial_nn_params = [initial_Theta1(:) ; initial_Theta2(:)];

%% =================== Part 8: Training NN ===================
%  You have now implemented all the code necessary to train a neural 
%  network. To train your neural network, we will now use "fmincg", which
%  is a function which works similarly to "fminunc". Recall that these
%  advanced optimizers are able to train our cost functions efficiently as
%  long as we provide them with the gradient computations.
%
fprintf('\nTraining Neural Network... \n')

%  After you have completed the assignment, change the MaxIter to a larger
%  value to see how more training helps.
options = optimset('MaxIter', maxIter);

%  You should also try different values of lambda
lambda = -0.3;

% Create "short hand" for the cost function to be minimized
costFunction = @(p) nnCostFunction(p, ...
                                   input_layer_size, ...
                                   hidden_layer_size, ...
                                   num_labels, X, y, lambda);
								  

% Now, costFunction is a function that takes in only one argument (the
% neural network parameters)
[nn_params, cost] = fmincg(costFunction, initial_nn_params, options);

% Obtain Theta1 and Theta2 back from nn_params
Theta1 = reshape(nn_params(1:hidden_layer_size * (input_layer_size + 1)), ...
                 hidden_layer_size, (input_layer_size + 1));

Theta2 = reshape(nn_params((1 + (hidden_layer_size * (input_layer_size + 1))):end), ...
                 num_labels, (hidden_layer_size + 1));


if(nopause == 0)
	fprintf('Program paused. Press enter to continue.\n');
	pause;
endif


%% ================= Part 9: Visualize Weights =================
%  You can now "visualize" what the neural network is learning by 
%  displaying the hidden units to see what features they are capturing in 
%  the data.

if(nographic == 0)
	fprintf('\nVisualizing Neural Network... \n')
	displayData(Theta1(:, 2:end));
endif

if(nopause == 0)
	fprintf('\nProgram paused. Press enter to continue.\n');
	pause;
endif

%% ================= Part 10: Implement Predict =================
%  After training the neural network, we would like to use it to predict
%  the labels. You will now implement the "predict" function to use the
%  neural network to predict the labels of the training set. This lets
%  you compute the training set accuracy.


pred = predict(Theta1, Theta2, X);
%conf = confusion_matrix(y, pred, num_labels, m);
fprintf('\nTraining Set Accuracy: %0.2f\n', mean(double(pred == y)) * 100);

collect_results("result_trening.txt", y, pred, num_labels, m);
% sacuvaj parametre
save("-binary", "thetas.mat", "Theta1", "Theta2");
